﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Outcoder.UI.Xaml.Controls
{
	[StyleTypedProperty(Property = "WatermarkTextStyle", StyleTargetType = typeof(TextBlock)),
	 TemplatePart(Name = "WatermarkText", Type = typeof(TextBlock)), 
	 TemplateVisualState(Name = "WatermarkTextVisible", GroupName = "WatermarkTextStates"), 
	 TemplateVisualState(Name = "WatermarkTextHidden", GroupName = "WatermarkTextStates")]
	public class WatermarkedTextBox : TextBox
	{
		public static readonly DependencyProperty WatermarkTextProperty =
			DependencyProperty.Register("WatermarkText",
				typeof(string),
				typeof(WatermarkedTextBox),
				new PropertyMetadata("", OnWatermarkTextPropertyChanged));

		public static readonly DependencyProperty WatermarkTextForegroundProperty =
			DependencyProperty.Register("WatermarkTextForeground",
				typeof(Brush),
				typeof(WatermarkedTextBox),
				new PropertyMetadata(new SolidColorBrush(Colors.Gray),
					OnWatermarkTextForegroundPropertyChanged));

		public static readonly DependencyProperty WatermarkTextStyleProperty =
			DependencyProperty.Register("WatermarkTextStyle",
				typeof(Style),
				typeof(WatermarkedTextBox),
				new PropertyMetadata(null, OnWatermarkTextStylePropertyChanged));

		bool focused;

		public string WatermarkText
		{
			get
			{
				return (string)GetValue(WatermarkTextProperty);
			}
			set
			{
				SetValue(WatermarkTextProperty, value);
			}
		}

		public Brush WatermarkTextForeground
		{
			get
			{
				return (Brush)GetValue(WatermarkTextForegroundProperty);
			}
			set
			{
				SetValue(WatermarkTextForegroundProperty, value);
			}
		}

		public Style WatermarkTextStyle
		{
			get
			{
				return (Style)GetValue(WatermarkTextStyleProperty);
			}
			set
			{
				SetValue(WatermarkTextStyleProperty, value);
			}
		}

		static void OnWatermarkTextPropertyChanged(
			DependencyObject theTarget,
			DependencyPropertyChangedEventArgs theDependencyPropertyChangedEventArgs)
		{
		}

		static void OnWatermarkTextForegroundPropertyChanged(
			DependencyObject theTarget,
			DependencyPropertyChangedEventArgs theDependencyPropertyChangedEventArgs)
		{
		}

		static void OnWatermarkTextStylePropertyChanged(
			DependencyObject theTarget,
			DependencyPropertyChangedEventArgs theDependencyPropertyChangedEventArgs)
		{
		}

		public WatermarkedTextBox()
		{
			DefaultStyleKey = typeof(WatermarkedTextBox);

			GotFocus += WatermarkTextBox_GotFocus;
			LostFocus += WatermarkTextBox_LostFocus;
			Loaded += WatermarkTextBox_Loaded;
			TextChanged += WatermarkTextBox_TextChanged;
		}

		void WatermarkTextBox_Loaded(object sender, RoutedEventArgs e)
		{
			GoToVisualState(true);
		}

		void WatermarkTextBox_GotFocus(object sender, RoutedEventArgs e)
		{
			focused = true;
			GoToVisualState(false);
		}

		void WatermarkTextBox_LostFocus(object sender, RoutedEventArgs e)
		{
			focused = false;
			GoToVisualState(true);
		}

		void WatermarkTextBox_TextChanged(object sender, TextChangedEventArgs e)
		{
			if (!focused)
			{
				GoToVisualState(false);
			}
		}

		void GoToVisualState(bool shouldShowWatermark)
		{
			if (shouldShowWatermark && string.IsNullOrEmpty(Text))
			{
				VisualStateManager.GoToState(this, "WatermarkTextVisible", true);
			}
			else
			{
				VisualStateManager.GoToState(this, "WatermarkTextHidden", true);
			}
		}
	}
}